package com.yk.mr.dao.cluster.impl;

import com.yk.mr.dao.cluster.api.IClusterDao;
import com.yk.mr.dao.common.api.ICommonDAO;
import com.yk.mr.model.cluster.ClusterModel;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;

/**
 * Project Name:cykspark
 * File Name:ClusterDaoImpl
 * Package Name:com.yk.spark.dao.cluster.impl
 * Date:2017/9/20 10:31
 * Author:zhangju
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */

@Repository("clusterDao")
public class ClusterDaoImpl implements IClusterDao {
    @Resource(name = "commonDAO")
    private ICommonDAO commonDAO;

    /**
     * 获取所有集群信息
     *
     * @return 集群信息列表
     */
    @Override
    public List<ClusterModel> selectAll() {
        String hql="from ClusterModel  as model";
        Query<ClusterModel> query=this.commonDAO.getSession().createQuery(hql,ClusterModel.class);
        return query.list();
    }

    /**
     * 分页获取集群信息
     *
     * @param start 起始位置
     * @param limit 最大结果数
     * @return 集群信息列表
     */
    @Override
    public List<ClusterModel> select(int start, int limit) {

        String hql="from ClusterModel  as model";
        Query<ClusterModel> query=this.commonDAO.getSession().createQuery(hql,ClusterModel.class);
        query.setFirstResult(start);
        query.setMaxResults(limit);
        return query.list();
    }

    /**
     * 查询记录总数
     *
     * @return 总记录数
     */
    @Override
    public Long total() {
        String hql="select count (*) from ClusterModel  as model";
        Query<Long> query=this.commonDAO.getSession().createQuery(hql,Long.class);
        return query.getSingleResult();
    }

    /**
     * 根据clusterId 获取集群信息实体对象
     *
     * @param model 只包含clusterId 的集群信息实体对象
     * @return 集群信息实体对象
     */
    @Override
    public ClusterModel select(ClusterModel model) {

        String hql="from ClusterModel  as model where model.clusterId=:clusterId";
        Query<ClusterModel> query=this.commonDAO.getSession().createQuery(hql,ClusterModel.class);
        query.setParameter("clusterId",model.getClusterId());
        return query.getSingleResult();
    }

    /**
     * 根据clusterName或ip模糊查询集群信息
     *
     * @param model 包含clusterName或ip的集群信息实体对象
     * @return 集群信息列表
     */
    @Override
    public List<ClusterModel> search(ClusterModel model) {

        String baseHql="from ClusterModel as model where";
        HashSet<String> conditions=new HashSet<>();
        if(model.getClusterName()!=null&&!model.getClusterName().isEmpty()){
            String clusterNameCondition="model.clusterName like '%"+model.getClusterName()+"%'";
            conditions.add(clusterNameCondition);
        }

        if(model.getIp()!=null&&!model.getIp().isEmpty()){
            String clusterIpCondition="model.ip like '%"+model.getIp()+"%'";
            conditions.add(clusterIpCondition);
        }
        Query<ClusterModel> query=this.commonDAO.getSession().createQuery(baseHql+this.commonDAO.getOrCondition(conditions),ClusterModel.class);
        return query.list();
    }

    @Override
    public ClusterModel queryByClusterId(String clusterId) {
        String hql ="from ClusterModel as model where model.clusterId=:clusterId";
        Query<ClusterModel> query=this.commonDAO.getNewSession().createQuery(hql,ClusterModel.class);
        query.setParameter("clusterId",clusterId);
        return query.getSingleResult();
    }
}
